package com.pwc.conversionTool.dataAdaptor;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.Iterator;
import java.util.List;

import com.pwc.conversionTool.entity.FilterCondition;
import com.pwc.conversionTool.entity.Formula;

public class DataFilter {

	private List<String[]> tempRows = new ArrayList<String[]>();
//	private List<FilterCondition> dataFilterList;
	private IDataAdaptor allData;
	private Formula formula;



	public DataFilter(IDataAdaptor allData, Formula formulaInput) {
		this.allData = allData;
		this.tempRows = allData.getContent();
		this.formula=formulaInput;
	//this.dataFilterList = formulaInput.getFilter();
	}

	public IDataAdaptor Process() {
		
		if(formula!=null)
		{
			if (formula.getFilter() != null) {
				Iterator<FilterCondition> iterator = formula.getFilter().iterator();
				while (iterator.hasNext()) {
					FilterCondition oneFilterCondition = iterator.next();
					//string
					if (oneFilterCondition.getOperation().equals("equal")) {
						equalsFunction(oneFilterCondition.getColumnName(),
								oneFilterCondition.getValue(), allData, tempRows);
					}
					if (oneFilterCondition.getOperation().equals("contains")) {
						containsFunction(oneFilterCondition.getColumnName(),
								oneFilterCondition.getValue(), allData, tempRows);
					}
					if (oneFilterCondition.getOperation().equals("notContains")) {
						notcontainsFunction(oneFilterCondition.getColumnName(),
								oneFilterCondition.getValue(), allData, tempRows);
					}
					if (oneFilterCondition.getOperation().equals("notContains")) {
						startwithFunction(oneFilterCondition.getColumnName(),
								oneFilterCondition.getValue(), allData, tempRows);
					}
					if (oneFilterCondition.getOperation().equals("notContains")) {
						endwithFunction(oneFilterCondition.getColumnName(),
								oneFilterCondition.getValue(), allData, tempRows);
					}
					
					//number
					if (oneFilterCondition.getOperation().equals("lessThan")) {
						smallerNumFunction(oneFilterCondition.getColumnName(),
								oneFilterCondition.getValue(), allData, tempRows);
					}
					if (oneFilterCondition.getOperation().equals("greaterThan")) {
						largerNumFunction(oneFilterCondition.getColumnName(),
								oneFilterCondition.getValue(), allData, tempRows);
					}
					if (oneFilterCondition.getOperation().equals("equalsNum")) {
						equalsNumFunction(oneFilterCondition.getColumnName(),
								oneFilterCondition.getValue(), allData, tempRows);
					}
					if (oneFilterCondition.getOperation().equals("notLessThan")) {
						largerequalNumFunction(oneFilterCondition.getColumnName(),
								oneFilterCondition.getValue(), allData, tempRows);
					}
					if (oneFilterCondition.getOperation().equals("notGreaterThan")) {
						smallequalNumFunction(oneFilterCondition.getColumnName(),
								oneFilterCondition.getValue(), allData, tempRows);
					}
					//date
					if (oneFilterCondition.getOperation().equals("equalDate")) {
						equalDateFunction(oneFilterCondition.getColumnName(),
								oneFilterCondition.getValue(), allData, tempRows);
					}
					if (oneFilterCondition.getOperation().equals("laterThan")) {
						laterDateFunction(oneFilterCondition.getColumnName(),
								oneFilterCondition.getValue(), allData, tempRows);
					}
					if (oneFilterCondition.getOperation().equals("earlierThan")) {
						earlierDateFunction(oneFilterCondition.getColumnName(),
								oneFilterCondition.getValue(), allData, tempRows);
					}
					if (oneFilterCondition.getOperation().equals("notEarlierThan")) {
						laterequalDateFunction(oneFilterCondition.getColumnName(),
								oneFilterCondition.getValue(), allData, tempRows);
					}
					if (oneFilterCondition.getOperation().equals("notLaterThan")) {
						earlierequalDateFunction(oneFilterCondition.getColumnName(),
								oneFilterCondition.getValue(), allData, tempRows);
					}
				}
			}

		}

		allData.setContent(tempRows);

		return allData;
	}

	/*
	 * String filter: equal,notequal,contains,notContains,startwith,endwith.
	 */
	public static void equalsFunction(String columnName, String OpeValue,
			IDataAdaptor allData, List<String[]> tempRows) {

		System.out
				.println("----------------------equalsFunction---------------------------");
		Iterator<String[]> iterator = tempRows.iterator();
		while (iterator.hasNext()) {
			String[] i = iterator.next();
			int index = allData.getColumnIndex(columnName);
			if (!OpeValue.equals(i[index])
					&& !allData.getHeader()[index].equals(i[index])) {
				iterator.remove();

			}

		}

	}

	public static void notequalsFunction(String columnName, String OpeValue,
			IDataAdaptor allData, List<String[]> tempRows) {
		System.out
				.println("----------------------notequalsFunction---------------------------");
		Iterator<String[]> iterator = tempRows.iterator();
		while (iterator.hasNext()) {
			String[] i = iterator.next();
			int index = allData.getColumnIndex(columnName);
			if (OpeValue.equals(i[index])
					&& !allData.getHeader()[index].equals(i[index])) {
				iterator.remove();

			}

		}
	}

	public static void containsFunction(String columnName, String OpeValue,
			IDataAdaptor allData, List<String[]> tempRows) {
		System.out
				.println("-------------------containsFunction------------------------------");
		Iterator<String[]> iterator = tempRows.iterator();
		while (iterator.hasNext()) {
			String[] i = iterator.next();
			int index = allData.getColumnIndex(columnName);
			if (!i[index].contains(OpeValue)
					&& !allData.getHeader()[index].equals(i[index])) {
				iterator.remove();

			}

		}
	}

	public static void notcontainsFunction(String columnName, String OpeValue,
			IDataAdaptor allData, List<String[]> tempRows) {
		System.out
				.println("--------------------notcontainsFunction-----------------------------");
		Iterator<String[]> iterator = tempRows.iterator();
		while (iterator.hasNext()) {
			String[] i = iterator.next();
			int index = allData.getColumnIndex(columnName);
			if (i[index].contains(OpeValue)
					&& !allData.getHeader()[index].equals(i[index])) {
				iterator.remove();

			}

		}
	}

	public static void startwithFunction(String columnName, String OpeValue,
			IDataAdaptor allData, List<String[]> tempRows) {
		System.out
				.println("-------------------startwithFunction------------------------------");
		Iterator<String[]> iterator = tempRows.iterator();
		while (iterator.hasNext()) {
			String[] i = iterator.next();
			int index = allData.getColumnIndex(columnName);
			if (!i[index].startsWith(OpeValue)
					&& !allData.getHeader()[index].equals(i[index])) {
				iterator.remove();

			}

		}
	}

	public static void endwithFunction(String columnName, String OpeValue,
			IDataAdaptor allData, List<String[]> tempRows) {
		System.out
				.println("-----------------------endwithFunction--------------------------");
		Iterator<String[]> iterator = tempRows.iterator();
		while (iterator.hasNext()) {
			String[] i = iterator.next();
			int index = allData.getColumnIndex(columnName);
			if (!i[index].endsWith(OpeValue)
					&& !allData.getHeader()[index].equals(i[index])) {
				iterator.remove();

			}

		}
	}

	/*
	 *  Number function lessThan,greaterThan,noLessThan,noGreaterThan.
	 */

	public static void largerNumFunction(String columnName, String OpeValue,
			IDataAdaptor allData, List<String[]> tempRows) {
		System.out
				.println("-----------------------largerNumFunction--------------------------");

		Iterator<String[]> iterator = tempRows.iterator();
		if (iterator.hasNext()) {
			iterator.next();
		}

		while (iterator.hasNext()) {
			String[] i = iterator.next();
			int index = allData.getColumnIndex(columnName);

			Float numberN;
			Float numberOrigin;
			String flag = null;
			try {
				numberN = Float.parseFloat(OpeValue);
				numberOrigin = Float.parseFloat(i[index]);

				if (numberN < numberOrigin) {
					flag = "Y";
				}
			} catch (NumberFormatException e) {
				// TODO Auto-generated catch block
				iterator.remove();
				continue;
			}

			System.out.println(flag);
			if (flag == null && !allData.getHeader()[index].equals(i[index])) {
				iterator.remove();
			}

		}

	}

	public static void smallerNumFunction(String columnName, String OpeValue,
			IDataAdaptor allData, List<String[]> tempRows) {
		System.out
				.println("-----------------------smallerNumFunction--------------------------");

		Iterator<String[]> iterator = tempRows.iterator();
		if (iterator.hasNext()) {
			iterator.next();
		}

		while (iterator.hasNext()) {
			String[] i = iterator.next();
			int index = allData.getColumnIndex(columnName);
			Float numberN;
			Float numberOrigin;
			String flag = null;
			try {
				numberN = Float.parseFloat(OpeValue);
				numberOrigin = Float.parseFloat(i[index]);
				if (numberN > numberOrigin) {
					flag = "Y";
				}
			} catch (NumberFormatException e) {
				// TODO Auto-generated catch block
				iterator.remove();
				continue;
			}

			if (flag == null && !allData.getHeader()[index].equals(i[index])) {
				iterator.remove();
			}

		}

	}

	public static void equalsNumFunction(String columnName, String OpeValue,
			IDataAdaptor allData, List<String[]> tempRows) {
		System.out
				.println("-----------------------equalsNumFunction--------------------------");

		Iterator<String[]> iterator = tempRows.iterator();
		if (iterator.hasNext()) {
			iterator.next();
		}

		while (iterator.hasNext()) {
			String[] i = iterator.next();
			int index = allData.getColumnIndex(columnName);
			Float numberN;
			Float numberOrigin;
			String flag = null;

			try {
				numberN = Float.parseFloat(OpeValue);
				numberOrigin = Float.parseFloat(i[index]);
				flag = null;
				if (numberN == numberOrigin) {
					flag = "Y";
				}
			} catch (NumberFormatException e) {
				// TODO Auto-generated catch block
				iterator.remove();
				continue;

			}
			if (flag == null && !allData.getHeader()[index].equals(i[index])) {
				iterator.remove();
			}

		}

	}

	public static void largerequalNumFunction(String columnName,
			String OpeValue, IDataAdaptor allData, List<String[]> tempRows) {
		System.out
				.println("-----------------------largerequalNumFunction--------------------------");

		Iterator<String[]> iterator = tempRows.iterator();
		if (iterator.hasNext()) {
			iterator.next();
		}

		while (iterator.hasNext()) {
			String[] i = iterator.next();
			int index = allData.getColumnIndex(columnName);
			Float numberN;
			Float numberOrigin;
			String flag = null;
			try {
				numberN = Float.parseFloat(OpeValue);
				numberOrigin = Float.parseFloat(i[index]);
				if (numberN <= numberOrigin) {
					flag = "Y";
				}
			} catch (NumberFormatException e) {
				// TODO Auto-generated catch block
				iterator.remove();
				continue;
			}

			if (flag == null && !allData.getHeader()[index].equals(i[index])) {
				iterator.remove();
			}

		}

	}

	public static void smallequalNumFunction(String columnName,
			String OpeValue, IDataAdaptor allData, List<String[]> tempRows) {
		System.out
				.println("-----------------------smallequalNumFunction--------------------------");

		Iterator<String[]> iterator = tempRows.iterator();
		if (iterator.hasNext()) {
			iterator.next();
		}

		while (iterator.hasNext()) {
			String[] i = iterator.next();
			int index = allData.getColumnIndex(columnName);
			Float numberN;
			Float numberOrigin;
			String flag = null;
			try {
				numberN = Float.parseFloat(OpeValue);
				numberOrigin = Float.parseFloat(i[index]);
				if (numberN >= numberOrigin) {
					flag = "Y";
				}
			} catch (NumberFormatException e) {
				// TODO Auto-generated catch block

				iterator.remove();
				continue;
			}

			if (flag == null && !allData.getHeader()[index].equals(i[index])) {
				iterator.remove();
			}

		}

	}

	/*
	 *  Date functions:equalDate,laterDate,earlierDate,noLaterDate,noEarlierDate
	 */

	public static void equalDateFunction(String columnName,
			String OpeValue, IDataAdaptor allData, List<String[]> tempRows) {
		System.out
				.println("-----------------------equalDateFunction--------------------------");

		Iterator<String[]> iterator = tempRows.iterator();
		if (iterator.hasNext()) {
			iterator.next();
		}

		while (iterator.hasNext()) {
			String[] i = iterator.next();
			int index = allData.getColumnIndex(columnName);

			SimpleDateFormat bartDateFormat = new SimpleDateFormat("yyyy-MM-dd");
			Date date = new Date();
			Date date2 = new Date();
			try {
				date = bartDateFormat.parse(OpeValue);
				date2 = bartDateFormat.parse(i[index]);
			} catch (ParseException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			String flag = null;
			if (date.equals(date2)) {
				flag = "Y";
			}
			if (flag == null && !allData.getHeader()[index].equals(i[index])) {
				iterator.remove();
			}

		}

	}

	public static void laterDateFunction(String columnName,
			String OpeValue, IDataAdaptor allData, List<String[]> tempRows) {
		System.out
				.println("-----------------------laterDateFunction--------------------------");

		Iterator<String[]> iterator = tempRows.iterator();
		if (iterator.hasNext()) {
			iterator.next();
		}

		while (iterator.hasNext()) {
			String[] i = iterator.next();
			int index = allData.getColumnIndex(columnName);

			SimpleDateFormat bartDateFormat = new SimpleDateFormat("yyyy-MM-dd");
			Date date = new Date();
			Date date2 = new Date();
			try {
				date = bartDateFormat.parse(OpeValue);
				date2 = bartDateFormat.parse(i[index]);
			} catch (ParseException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			String flag = null;
			System.out.println(date);
			System.out.println(date2);
			if (date2.after(date)) {
				flag = "Y";
			}
			System.out.println(flag);
			if (flag == null && !allData.getHeader()[index].equals(i[index])) {
				iterator.remove();
			}

		}

	}

	public static void earlierDateFunction(String columnName,
			String OpeValue, IDataAdaptor allData, List<String[]> tempRows) {
		System.out
				.println("-----------------------earlierDateFunction--------------------------");

		Iterator<String[]> iterator = tempRows.iterator();
		if (iterator.hasNext()) {
			iterator.next();
		}

		while (iterator.hasNext()) {
			String[] i = iterator.next();
			int index = allData.getColumnIndex(columnName);

			SimpleDateFormat bartDateFormat = new SimpleDateFormat("yyyy-MM-dd");
			Date date = new Date();
			Date date2 = new Date();
			try {
				date = bartDateFormat.parse(OpeValue);
				date2 = bartDateFormat.parse(i[index]);
			} catch (ParseException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			String flag = null;
			System.out.println(date);
			System.out.println(date2);
			if (date2.before(date)) {
				flag = "Y";
			}
			System.out.println(flag);
			if (flag == null && !allData.getHeader()[index].equals(i[index])) {
				iterator.remove();
			}

		}

	}

	public static void laterequalDateFunction(String columnName,
			String OpeValue, IDataAdaptor allData, List<String[]> tempRows) {
		System.out
				.println("-----------------------laterequalDateFunction--------------------------");

		Iterator<String[]> iterator = tempRows.iterator();
		if (iterator.hasNext()) {
			iterator.next();
		}

		while (iterator.hasNext()) {
			String[] i = iterator.next();
			int index = allData.getColumnIndex(columnName);

			SimpleDateFormat bartDateFormat = new SimpleDateFormat("yyyy-MM-dd");
			Date date = new Date();
			Date date2 = new Date();
			try {
				date = bartDateFormat.parse(OpeValue);
				date2 = bartDateFormat.parse(i[index]);
			} catch (ParseException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			String flag = null;
			System.out.println(date);
			System.out.println(date2);
			if (date2.after(date) | date2.equals(date)) {
				flag = "Y";
			}
			System.out.println(flag);
			if (flag == null && !allData.getHeader()[index].equals(i[index])) {
				iterator.remove();
			}

		}

	}

	public static void earlierequalDateFunction(String columnName,
			String OpeValue, IDataAdaptor allData, List<String[]> tempRows) {
		System.out
				.println("-----------------------earlierequalDateFunction--------------------------");

		Iterator<String[]> iterator = tempRows.iterator();
		if (iterator.hasNext()) {
			iterator.next();
		}

		while (iterator.hasNext()) {
			String[] i = iterator.next();
			int index = allData.getColumnIndex(columnName);

			SimpleDateFormat bartDateFormat = new SimpleDateFormat("yyyy-MM-dd");
			Date date = new Date();
			Date date2 = new Date();
			try {
				date = bartDateFormat.parse(OpeValue);
				date2 = bartDateFormat.parse(i[index]);
			} catch (ParseException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			String flag = null;
			System.out.println(date);
			System.out.println(date2);
			if (date2.before(date) | date2.equals(date)) {
				flag = "Y";
			}
			System.out.println(flag);
			if (flag == null && !allData.getHeader()[index].equals(i[index])) {
				iterator.remove();
			}

		}

	}
	
	
	public Formula getFormula() {
		return formula;
	}

	public void setFormula(Formula formula) {
		this.formula = formula;
	}

}
